<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents the use of the GNU compilers.

Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Fast enumeration details (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Fast enumeration details (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Fast enumeration details (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Fast-enumeration.html" rel="up" title="Fast enumeration">
<link href="Fast-enumeration-protocol.html" rel="next" title="Fast enumeration protocol">
<link href="c99_002dlike-fast-enumeration-syntax.html" rel="prev" title="c99-like fast enumeration syntax">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>


</head>

<body lang="en_US">
<div class="subsection-level-extent" id="Fast-enumeration-details">
<div class="nav-panel">
<p>
Next: <a href="Fast-enumeration-protocol.html" accesskey="n" rel="next">Fast Enumeration Protocol</a>, Previous: <a href="c99_002dlike-fast-enumeration-syntax.html" accesskey="p" rel="prev">C99-Like Fast Enumeration Syntax</a>, Up: <a href="Fast-enumeration.html" accesskey="u" rel="up">Fast Enumeration</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Fast-Enumeration-Details"><span>8.9.3 Fast Enumeration Details<a class="copiable-link" href="#Fast-Enumeration-Details"> &para;</a></span></h4>

<p>Here is a more technical description with the gory details.  Consider the code
</p>
<div class="example smallexample">
<pre class="example-preformatted">  for (<var class="var">object expression</var> in <var class="var">collection expression</var>)
  {
    <var class="var">statements</var>
  }
</pre></div>

<p>here is what happens when you run it:
</p>
<ul class="itemize mark-bullet">
<li><code class="code"><var class="var">collection expression</var></code> is evaluated exactly once and the
result is used as the collection object to iterate over.  This means
it is safe to write code such as <code class="code">for (object in [NSDictionary
keyEnumerator]) &hellip;</code>.

</li><li>the iteration is implemented by the compiler by repeatedly getting
batches of objects from the collection object using the fast
enumeration protocol (see below), then iterating over all objects in
the batch.  This is faster than a normal enumeration where objects are
retrieved one by one (hence the name &ldquo;fast enumeration&rdquo;).

</li><li>if there are no objects in the collection, then
<code class="code"><var class="var">object expression</var></code> is set to <code class="code">nil</code> and the loop
immediately terminates.

</li><li>if there are objects in the collection, then for each object in the
collection (in the order they are returned) <code class="code"><var class="var">object expression</var></code>
is set to the object, then <code class="code"><var class="var">statements</var></code> are executed.

</li><li><code class="code"><var class="var">statements</var></code> can contain <code class="code">break</code> and <code class="code">continue</code>
commands, which will abort the iteration or skip to the next loop
iteration as expected.

</li><li>when the iteration ends because there are no more objects to iterate
over, <code class="code"><var class="var">object expression</var></code> is set to <code class="code">nil</code>.  This allows
you to determine whether the iteration finished because a <code class="code">break</code>
command was used (in which case <code class="code"><var class="var">object expression</var></code> will remain
set to the last object that was iterated over) or because it iterated
over all the objects (in which case <code class="code"><var class="var">object expression</var></code> will be
set to <code class="code">nil</code>).

</li><li><code class="code"><var class="var">statements</var></code> must not make any changes to the collection
object; if they do, it is a hard error and the fast enumeration
terminates by invoking <code class="code">objc_enumerationMutation</code>, a runtime
function that normally aborts the program but which can be customized
by Foundation libraries via <code class="code">objc_set_mutation_handler</code> to do
something different, such as raising an exception.

</li></ul>

</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Fast-enumeration-protocol.html">Fast Enumeration Protocol</a>, Previous: <a href="c99_002dlike-fast-enumeration-syntax.html">C99-Like Fast Enumeration Syntax</a>, Up: <a href="Fast-enumeration.html">Fast Enumeration</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
